<?php

namespace app\index\controller;

use think\Controller;
use think\Facade\Request;
use app\index\model\Order as Model;
use app\index\model\Customer;
use Units\RoutePlanner;
use Units\Area;
use app\index\model\Order as OrderModel;
use Units\ChunkRouter;
use app\index\model\Routers;
use think\Db;
use Units\TransformOrderField;
use app\index\model\Vehicle;
use Units\Http;
use think\Facade\Config;

class Order extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        if(Request::isPost()){
            $today = date('Y-m-d');
            $where['Day'] = $today;
            $CustName = input('post.CustName');
            $WareHouse = Request::post('WareHouse');
            if($CustName!=''){
                $where[] = ['CustName', 'like', "%{$CustName}%"];
            }

            $where['WareHouse'] = $WareHouse;
            $order = Model::where($where)->order('positionStatus asc')->paginate(10);
            $total= Model::where($where)->fetchSql(false)->count();
            //统计经纬度错误
            $error_positon = Model::where(['positionStatus'=>0,'WareHouse'=>$WareHouse,'Day'=>$today])
                ->count();
            $data = $order->items();
            returnAjax('200',[
                'data'=>$data,
                'total'=>$total,
                'page'=>input('get.page',1),
                'error_position'=>$error_positon
            ]);
        }
        return $this->fetch();
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function all()
    {
        if(Request::isPost()){
            $wareHouse = Request::post('wareHouse');
            $data = Model::where(['wareHouse'=>$wareHouse,'Day'=>date('Y-m-d')])
                ->field('lat,lng,CustID,Address,City,GrossWeight,id,ScheduleID,TimeOnSite,ScheduleTime,CustomerTag,CustName')
               // ->limit(2)
                ->select();
            returnAjax('200',$data);
        }
        return $this->fetch();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
         if( Request::isPost() ) {

         }
         return $this->fetch();
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }

    public function checkUnAssignPoints()
    {
        $WareHouse = Request::post('WareHouse');
        $count = Model::where(['WareHouse'=>$WareHouse,'Day'=>date('Y-m-d')])->count();
        if( $count )
        {
            returnAjax('200',$count);
        }else{
            returnAjax('201',$count);
        }
    }

    public function getEmptyLatAndLng()
    {
        $WareHouse = Request::post('WareHouse');
        $count = Model::where([
            'Day'=>date('Y-m-d'),
            'isUpdateLatAndLng'=>0,
            'WareHouse'=>$WareHouse
        ])->count();
        returnAjax('200',$count);
    }

    /**
     * 重SAP获取订单数据
     */
    public function loadDataFromSap()
    {
        $wareHouse = Request::get('warehouse');
        /*从SAP读取数据*/
       // $orders = Http::instance()->request(Config::get('SAP_API_URL'))->toArray();
        // var_dump($orders);die;
        //插入order表
        $orders = Db::table('tb_120_invoice_tms')->select();
        foreach ( $orders as $key=> $item ) {
            $newOrders = TransformOrderField::instance()->transformField($item);
            OrderModel::create($newOrders);
        }

        returnAjax('200','SUCCESS');

    }
}
